package batch.process.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

public class BatchProcessProperties {
    private static PropertiesConfiguration _props = new PropertiesConfiguration(); 

    public static void load() throws Exception {
    	String mainHome_dir = System.getProperty(Constant.MAIN_HOME_PATH_SYS_VAR);
    	_props.addProperty(Constant.MAIN_HOME_PATH_STR, mainHome_dir);
    }
    
    public static void load(String propFile) throws Exception {
    	try {
    		load();
    		
    		InputStream is = (BatchProcessProperties.class).
				getClassLoader().getResourceAsStream(propFile);
	
			if (is == null) {			   
				is = new FileInputStream((
						new File(propFile)).getAbsoluteFile());
			}
				
			_props.load(is);
			is.close();
        } catch (ConfigurationException e) {			
			e.printStackTrace();
			throw(e);
		} catch (FileNotFoundException e) {			
			e.printStackTrace();
			throw(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw(e);
		}
    		
    }
    
    public static String getString(String key, String defaultValue){
        return _props.getString(key, defaultValue);
    }
    
    public static String getString(String key){
        return _props.getString(key);
    }
 
    
    public static Integer getInteger(String key, Integer def){
        Integer retorno = _props.getInt(key);
        if (retorno == null || retorno == 0)
        	return def;
        return retorno;
    }
    public static Integer getInteger(String key){
        return _props.getInt(key);
    }

    public static String getProperty(String key){
        return getString(key);
    }
    public static String getProperty(String key, String def){
        return getString(key,def);
    }

    
    @SuppressWarnings("unchecked")
	public static List<Object> getList(String key){
    	return  _props.getList(key);
    }
 

    public static Long getLong(String key){
        return _props.getLong(key);
    }
    public static Long getLong(String key, Long def){
        Long retorno = _props.getLong(key);
        if (retorno == null || retorno == 0)
        	return def;
        return retorno;
    }
        
    public static boolean getBoolean(String key){
        return(Boolean.valueOf((String)_props.getProperty(key)));        
    }
    

}
